﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class UserInfoDAL
    {
        /// <summary>
        /// 根据用户信息表id打印用户信息表数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(int Id)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                var res = con.UserInfos.Find(Id);
                return res;
            }
        }

        /// <summary>
        /// 根据前端发送的用户信息表id和用户信息修改用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UserinfoModify(UserInfoModify_DTO model)
        { 
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                UserInfo userInfo = con.UserInfos.FirstOrDefault(x => x.Id == model.Id);
                if(userInfo != null)
                {
                    userInfo.Name = model.Name;
                    userInfo.Signature = model.Signature;
                    userInfo.Region = model.Region;
                    userInfo.Gender = model.Gender;
                    userInfo.Avatar = model.Avatar;
                    con.UserInfos.Update(userInfo);
                    var count= con.SaveChanges();
                    return count > 0 ? 200 : 506;
                }
                else
                {
                    return 404;
                }
            }
        }
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetUserInfos()
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.UserInfos.ToList();
            }
        }
        public bool DeleteUserInfos(int id)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                UserInfo userInfo = con.UserInfos.Find(id);
                if(userInfo != null)
                {
                    con.UserInfos.Remove(userInfo);
                }
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool IsUserExists(int userId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.UserInfos.Any(u => u.Id == userId);
            }
        }
        /// <summary>
        /// 获取被关注的人员信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        public List<ConcernUserInfo_DTO> GetConcernUserInfos(int userInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                // 首先获取关注用户的基本信息
                var query = from a in con.Concerns.Where(x => x.UserInfoId == userInfoId)
                            join b in con.UserInfos on a.ConcernUserInfoId equals b.Id
                            select b;

                // 然后分别获取每个用户的视频数和关注数
                var result = query.Select(b => new ConcernUserInfo_DTO
                {
                    Id = b.Id,
                    Name = b.Name,
                    Avatar = b.Avatar,
                    Signature = b.Signature ?? "",
                    Content = con.VideoInfos.Count(v => v.UserInfoId == b.Id),
                    AFOS = con.Concerns.Count(c => c.ConcernUserInfoId == b.Id)
                });
                return result.ToList();
            }
        }
    }
}
